{{Extend "layout"}}

{{Block "title"}}{{"网站文件管理"|T}}{{/Block}}
{{Block "breadcrumb"}}
{{Super}}
<li class="active">{{"网站文件管理"|T}}: {{Stored.Get "absPath"}}</li>
{{/Block}}

{{Block "head"}}
<link rel="stylesheet" href="__ASSETS__/js/dropzone/min/dropzone.min.css" />
<!-- codemirror -->
<link rel="stylesheet" href="__ASSETS__/js/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="__ASSETS__/js/codemirror/addon/display/fullscreen.css">
<link rel="stylesheet" href="__ASSETS__/js/codemirror/theme/night.css">
<script src="__ASSETS__/js/codemirror/lib/codemirror.js"></script>
<script src="__ASSETS__/js/codemirror/addon/mode/loadmode.js"></script>
<script src="__ASSETS__/js/codemirror/mode/meta.js"></script>
<script src="__ASSETS__/js/codemirror/addon/display/fullscreen.js"></script>
{{/Block}}

{{Block "main"}}

<div class="row">
				<div class="col-md-12">
					<div class="block-flat">
						<div class="header">
							<div class="btn-group">
								<button type="button" id="uploadBtn" class="btn btn-primary">
                                    <i class="fa fa-upload"></i>
                                    {{"上传文件到当前目录"|T}}
                                </button>
								<button type="button" id="uploadZipBtn" class="btn btn-warning" data-popover="popover" data-original-title="" data-content="{{"仅支持上传zip压缩包。成功解压到当前目录后，该压缩包会被自动删除"|T}}" data-placement="top" data-trigger="hover">
                                    <i class="fa fa-upload"></i>
                                    {{"打包上传到当前目录"|T}}
                                </button>
							</div>
						</div>
						<div class="content">
							<div class="table-responsive">
							<table class="table no-border hover">
								<thead class="no-border">
									<tr>
										<th><strong>{{"名称"|T}}</strong></th>
										<th style="width:15%;"><strong>{{"修改日期"|T}}</strong></th>
										<th style="width:15%;"><strong>{{"类型"|T}}</strong></th>
										<th style="width:15%;"><strong>{{"大小"|T}}</strong></th>
										<th style="width:15%;" class="text-center"><strong>{{"操作"|T}}</strong></th>
									</tr>
								</thead>
								<tbody class="no-border-y" id="tbody-content">
                                    {{$path := Stored.Get "path"}}
                                    {{if $path}}
                                    <tr>
										<td>
                                            <i class="fa fa-folder-o"></i>
                                            <a title="{{"点击回到上一级"|T}}" href="/manage/vhost_file?id={{Stored.data.Id}}&path={{$path}}/.." style="font-size:18px;font-weight:bold">..</a>
                                        </td>
										<td></td>
										<td></td>
										<td></td>
										<td class="text-center"></td>
									</tr>
                                    {{end}}
                                    {{range $k,$v := Stored.dirs}}
									<tr>
										<td>
                                            {{if $v.IsDir}}
                                            <i class="fa fa-folder-o"></i>
                                            {{else}}
                                            <i class="fa fa-file-o"></i>
                                            {{end}}
                                            <a href="/manage/vhost_file?id={{Stored.data.Id}}&path={{if $path}}{{$path}}/{{end}}{{$v.Name}}">{{$v.Name}}</a>
                                            &nbsp;
                                            <a href="javascript:;" title="{{"重命名"|T}}" data-url="/manage/vhost_file?id={{Stored.data.Id}}&do=rename&path={{if $path}}{{$path}}/{{end}}{{$v.Name}}" onclick="fileRename(this,'{{$v.Name}}',{{if $v.IsDir}}true{{else}}false{{end}})">
                                            <i class="fa fa-pencil"></i>
                                            </a>
                                        </td>
										<td>{{$v.ModTime.Format "2006-01-02 15:04:05"}}</td>
										<td>{{if $v.IsDir}}{{"文件夹"|T}}{{else}}{{"文件"|T}}{{end}}</td>
										<td>{{if $v.IsDir}}-{{else}}{{$v.Size|FormatByte}}{{end}}</td>
										<td>
                                            <a title="{{"删除"|T}}" class="label label-danger" href="/manage/vhost_file?id={{Stored.data.Id}}&do=delete&path={{if $path}}{{$path}}/{{end}}{{$v.Name}}" onclick="return confirm('{{T "真的要删除“%v”吗？" $v.Name}}');">
                                            <i class="fa fa-times"></i>
                                            </a>
                                            {{if and (not $v.IsDir) (Editable $v.Name)}}
                                            &nbsp;
                                            <a title="{{"编辑"|T}}" class="label label-success" href="javascript:;" data-url="/manage/vhost_file?id={{Stored.data.Id}}&do=edit&path={{if $path}}{{$path}}/{{end}}{{$v.Name}}" onclick="fileEdit(this,'{{$v.Name}}')">
                                            <i class="fa fa-pencil"></i>
                                            </a>
                                            {{end}}
                                        </td>
									</tr>
                                    {{end}}
								</tbody>
							</table>		
							</div>
						</div>
					</div>				
				</div>
			</div>
{{/Block}}

{{Block "footer"}}
{{Modal "__TMPL__/manage/file.yaml"}}
{{Modal "__TMPL__/manage/file_zip.yaml"}}
{{Modal "__TMPL__/manage/file_edit.yaml"}}
{{Modal "__TMPL__/manage/file_rename.yaml"}}
<script type="text/javascript" src="__ASSETS__/js/dropzone/min/dropzone.min.js"></script>
<script>
function uploadURL(vhostId,currentPath){
    return '/manage/vhost_file?id='+vhostId+'&do=upload&path='+encodeURIComponent(currentPath);
}
function refreshList() {
    App.loading('show');
    $.get(window.location.href,{'_pjax':'tbody-content'},function(r){
        var e=$(r);
        $('#tbody-content').html(e.find('#tbody-content').html());
        App.loading('hide');
    },'html');
}
var dropzone,dropzoneZIP,editor;
function initDropzone() {
    $('#multi-upload-dropzone').dropzone({
        url:uploadURL('{{Stored.data.Id}}','{{Stored.path}}'),
        dictDefaultMessage:'{{"可以把文件拖到这里来进行上传哦"|T}}'
    });
    $('#multi-upload-zip-dropzone').dropzone({
        url:uploadURL('{{Stored.data.Id}}','{{Stored.path}}')+'&pipe=unzip',
        dictDefaultMessage:'{{"可以把文件拖到这里来进行上传哦"|T}}<p>{{"仅支持上传zip压缩包。成功解压到当前目录后，该压缩包会被自动删除"|T}}</p>',
        acceptedFiles:'application/zip,application/x-zip-compressed'
    });
    
    dropzone=$('#multi-upload-dropzone').get(0).dropzone;
    dropzoneZIP=$('#multi-upload-zip-dropzone').get(0).dropzone;
}
function initCodeMirrorEditor() {
    if(editor)return;
    editor = CodeMirror.fromTextArea($("#file-edit-content")[0], {
      lineNumbers: true,
      theme: "night",
      extraKeys: {
        "F11": function(cm) {
          cm.setOption("fullScreen", !cm.getOption("fullScreen"));
        },
        "Esc": function(cm) {
          if (cm.getOption("fullScreen")) cm.setOption("fullScreen", false);
        }
      }
    });
    editor.setOption('lineWrapping', true);
    editor.setSize('auto', 'auto');
    $('#file-edit-modal .modal-footer .btn-success').on('click',function(){
        var url=$(this).data('url');
        var enc=$('#use-encoding-open').val();
        if(!enc)enc='';
        $.post(url,{content:editor.getValue(),encoding:enc},function(r){
            if(r.Code!=1)return App.message({title: '{{"系统消息"|T}}', text: r.Info},false);
            return App.message({title: '{{"系统消息"|T}}', text: '{{"保存成功"|T}}'},false);
        },'json');
    });
    $('#file-edit-modal .modal-body').css('padding',0);
    $('#use-encoding-open').on('change',function(){
        var enc=$(this).val();
        fileReopen(enc);
    });
    
}

function fileReopen(encoding,url) {
    App.loading('show');
    if(url==null)url=$('#file-edit-modal .modal-footer .btn-success').data('url');
    $.get(url,{encoding:encoding},function(r){
        App.loading('hide');
        if(r.Code!=1)return App.message({title: '{{"系统消息"|T}}', text: r.Info},false);
        editor.setValue(r.Data);
    },'json');
}

function fileEdit(obj,file) {
    var url=$(obj).data('url');
    $('#file-edit-modal .modal-footer .btn-success').data('url',url);
    App.loading('show');
    $.get(url,{},function(r){
        App.loading('hide');
        if(r.Code!=1)return App.message({title: '{{"系统消息"|T}}', text: r.Info},false);
        $('#file-edit-modal .modal-header h3').html('{{"编辑文件"|T}}: '+file);
        $('#file-edit-modal').niftyModal('show',{
            afterOpen: function(modal) {
                editor.setValue(r.Data);
                codeMirrorChangeMode(editor,file);
            },
            afterClose: function(modal) {
                $('#use-encoding-open').find('option:selected').prop('selected',false);
            }
        });
    },'json');
}

function fileRename(obj,file,isDir) {
    var url=$(obj).data('url');
    $('#file-rename-modal .modal-footer .btn-primary:last').data('url',url);
    $('#file-rename-modal .modal-header h3').html((isDir ? '{{"修改文件夹名"|T}}' : '{{"修改文件名"|T}}')+': '+file);
    $('#file-rename-modal').niftyModal('show',{afterOpen:function(modal){
        $('#file-rename-input').val(file);
    }});
}
function codeMirrorChangeMode(editor,val) {
  var m, mode, spec;
  if (m = /.+\.([^.]+)$/.exec(val)) {
    var info = CodeMirror.findModeByExtension(m[1]);
    if (info) {
      mode = info.mode;
      spec = info.mime;
    }
  } else if (/\//.test(val)) {
    var info = CodeMirror.findModeByMIME(val);
    if (info) {
      mode = info.mode;
      spec = val;
    }
  } else {
    mode = spec = val;
  }
  if (mode) {
    editor.setOption("mode", spec);
    CodeMirror.autoLoadMode(editor, mode);
  } else {
    console.log("Could not find a mode corresponding to " + val);
  }
}
Dropzone.autoDiscover=false;
CodeMirror.modeURL = "__ASSETS__/js/codemirror/mode/%N/%N.js";
$(function(){
    initDropzone();
	$('#uploadBtn').on('click',function(event){
		$('#multi-upload-modal').niftyModal('show',{afterClose:function(modal){
            dropzone.removeAllFiles();
            refreshList();
        }});
	});
    $('#uploadZipBtn').on('click',function(event){
		$('#multi-upload-zip-modal').niftyModal('show',{afterClose:function(modal){
            dropzoneZIP.removeAllFiles();
            refreshList();
        }});
	});
    $(window).on('resize',function(){
        $('#file-edit-modal').css({height:$(window).height(),width:'100%','max-width':'100%',left:0,top:0,transform:'none'});
        $('#file-edit-form').css({height:$(window).height()-150,width:'100%','max-width':'100%',overflow:'auto'});
    });
    $(window).trigger('resize');
    initCodeMirrorEditor();
    $('#file-rename-modal .modal-footer .btn-primary:last').click(function(){
        var url=$(this).data('url');
        App.loading('show');
        $.post(url,{name:$('#file-rename-input').val()},function(r){
            App.loading('hide');
            if(r.Code!=1)return App.message({title: '{{"系统消息"|T}}', text: r.Info},false);
            App.message({title: '{{"系统消息"|T}}', text: '{{"保存成功"|T}}'},false);
            refreshList();
        },'json');
    });
});
</script>
{{/Block}}